import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '../store/user'

const routes = [
  {
    path: '/login',
    component: () => import('../views/Login.vue')
  },
  {
    path: '/',
    redirect: '/users'
  },
  {
    path: '/users',
    component: () => import('../views/UserManage.vue')
  },
  {
    path: '/departments',
    component: () => import('../views/DepartmentManage.vue')
  },
  {
    path: '/positions',
    component: () => import('../views/PositionManage.vue')
  },
  {
    path: '/salaries',
    component: () => import('../views/SalaryManage.vue')
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 全局前置守卫：未登录跳转登录页
router.beforeEach((to, from, next) => {
  const store = useUserStore()
  if (to.path !== '/login' && !store.isLogin) {
    next('/login')
  } else if (to.path === '/login' && store.isLogin) {
    next('/')
  } else {
    next()
  }
})

export default router 